package org.lex.swing;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.KeyStroke;

/**
 * test
 * 
 * @author Daniel Liu
 */
public class ActionBuilder extends AbstractAction {
	private ActionListener actionListener = null;

	public ActionBuilder() {
		super();
		this.actionListener = NoopActionListener.instance;
	}

	public ActionBuilder(String name) {
		this();
		this.setName(name);
	}

	public ActionBuilder(String name, int mnemonic) {
		this(name);
		this.setMnemonic(mnemonic);
	}

	public ActionBuilder(String name, char mnemonic) {
		this(name);
		this.setMnemonic(mnemonic);
	}

	public ActionBuilder setActionListener(ActionListener listener) {
		if (null == listener) {
			this.actionListener = NoopActionListener.instance;
		} else {
			this.actionListener = listener;
		}
		return this;
	}

	// getters & setters
	public ActionBuilder setAcceleratorKey(KeyStroke value) {
		this.putValue(Action.ACCELERATOR_KEY, value);
		return this;
	}

	public ActionBuilder setMnemonic(int value) {
		this.putValue(Action.MNEMONIC_KEY, value);
		return this;
	}

	/**
	 * This method is now obsolete, please use <code>setMnemonic(int)</code> to
	 * set the mnemonic for a button. This method is only designed to handle
	 * character values which fall between 'a' and 'z' or 'A' and 'Z'.
	 * 
	 * @param mnemonic
	 *            a char specifying the mnemonic value
	 * @see #setMnemonic(int)
	 */
	public ActionBuilder setMnemonic(char mnemonic) {
		int vk = mnemonic;
		if (vk >= 'a' && vk <= 'z')
			vk -= ('a' - 'A');
		return setMnemonic(vk);
	}

	/**
	 * ����AbstractAction.getName()���ܻ᷵��nullֵ ����������Ա�֤������null
	 */
	public String getName() {
		return this.toNoneNullString(this.getValue(Action.NAME));
	}

	public ActionBuilder setName(String name) {
		this.putValue(Action.NAME, name);
		return this;
	}

	public ActionBuilder setTooltip(String tooltip) {
		this.putValue(Action.SHORT_DESCRIPTION, tooltip);
		return this;
	}

	public boolean isSelected() {
		Object value = this.getValue(Action.SELECTED_KEY);
		if (null == value)
			return false;
		return Boolean.valueOf(String.valueOf(value)).booleanValue();
	}

	public ActionBuilder setSelected(boolean value) {
		this.putValue(Action.SELECTED_KEY, value);
		return this;
	}

	public ActionBuilder setIcon(Icon icon) {
		this.putValue(Action.SMALL_ICON, icon);
		return this;
	}

	public Icon getIcon() {
		return (Icon) this.getValue(Action.SMALL_ICON);
	}

	/**
	 * ������Ը�����������ṩ�Լ���ʵ��,����ʹ��setActionListener(ActionListener)
	 */
	public void actionPerformed(ActionEvent e) {
		this.actionListener.actionPerformed(e);
	}

	private String toNoneNullString(Object o) {
		if (null == o)
			return "";
		else
			return o.toString();
	}

	/*
	 * an action-listener that do nothing
	 */
	static class NoopActionListener implements ActionListener {
		static NoopActionListener instance = new NoopActionListener();

		public void actionPerformed(ActionEvent e) {
		}
	}
}
